package com.douyinkan.util;

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;

public class FileUtil {
    
    public static boolean isImageFile(String filename) {
        String lowerCase = filename.toLowerCase();
        return lowerCase.endsWith(".jpg") || lowerCase.endsWith(".jpeg") || 
               lowerCase.endsWith(".png") || lowerCase.endsWith(".gif") || 
               lowerCase.endsWith(".webp") || lowerCase.endsWith(".bmp");
    }
    
    public static boolean isVideoFile(String filename) {
        String lowerCase = filename.toLowerCase();
        return lowerCase.endsWith(".mp4") || lowerCase.endsWith(".avi") ||
               lowerCase.endsWith(".mov") || lowerCase.endsWith(".mkv") ||
               lowerCase.endsWith(".webm") || lowerCase.endsWith(".flv") ||
               lowerCase.endsWith(".wmv") || lowerCase.endsWith(".m4v");
    }

    public static boolean isAudioFile(String filename) {
        String lowerCase = filename.toLowerCase();
        return lowerCase.endsWith(".mp3") || lowerCase.endsWith(".wav") ||
               lowerCase.endsWith(".flac") || lowerCase.endsWith(".aac") ||
               lowerCase.endsWith(".ogg") || lowerCase.endsWith(".m4a") ||
               lowerCase.endsWith(".wma");
    }
    
    public static String getFileExtension(String filename) {
        int lastDotIndex = filename.lastIndexOf('.');
        if (lastDotIndex > 0) {
            return filename.substring(lastDotIndex + 1).toLowerCase();
        }
        return "";
    }
    
    public static String getFilenameWithoutExtension(String filename) {
        int lastDotIndex = filename.lastIndexOf('.');
        if (lastDotIndex > 0) {
            return filename.substring(0, lastDotIndex);
        }
        return filename;
    }
    
    public static Path getRelativePath(String basePath, String fullPath) {
        Path base = Paths.get(basePath);
        Path full = Paths.get(fullPath);
        return base.relativize(full);
    }
    
    public static boolean isValidMediaFile(String filename) {
        return isImageFile(filename) || isVideoFile(filename) || isAudioFile(filename);
    }
    
    public static String getContentType(String filename) {
        if (isVideoFile(filename)) {
            return "video";
        } else if (isImageFile(filename)) {
            return "image";
        } else if (isAudioFile(filename)) {
            return "audio";
        }
        return "unknown";
    }
}